<?php
	/* ******************************************************** */
	/* Arcans Project (2010)                                    */
	/* Jeu sous forme d'application web entièrement conçu       */
	/* et réalisé par Killian Le Maitre                         */
	/* Jeu sous licence GNU GPL v3 (voir NOTICE et COPYING)     */
	/* ******************************************************** */

	//****************************************
	//classe principale d'affichage de la page
	//****************************************
	class MainClass {
		//propriétés :
		private $infos;
		private $page_opt;
		private $chat;
		private $onload;
		private $messages;
		private $address;
		private $xmlinfo;
		private $header;
		private $entete;
		private $menu;
		private $page;
		private $level;
		//méthodes :
			//méthodes publiques :
		function __construct() {
			$infos_xml = new DomDocument();
			$infos_xml->load("xml/infos.xml");
			$seek_xml = $infos_xml->getElementsByTagName("seek")->item(0);
			$this->infos["seek"] = $seek_xml->getAttribute("value");
			//gestion des sessions
			$page = htmlentities($_GET["p"]);
			switch($page) {
				//validation de l'inscription
				case "ainsv" :
					$code = htmlentities($_GET["i1"]);
					$mysql = new MysqlClass(0);
					$result = $mysql->resultFun("count", "unvalid_users", "code='".$code."'");
					if ($result == 1) {
						$mysqlinsv = new MysqlClass(2);
						$result = $mysqlinsv->resultFun("select", "unvalid_users", "code='".$code."'");
						$pwd = md5(hash("sha256", $result["users"]).hash("sha256", $result["password"]));
						$mysqlinsv->resultFun("insert", "users", "(users,password,email,level) values('".$result["users"]."','".$pwd."','".$result["email"]."',0)");
						$mysqlinsv->resultFun("delete", "unvalid_users", "code='".$code."'");
						$_SESSION["pseudo"] = $result["users"];
						$res_id = $mysql->resultFun("select","users","users='".$_SESSION["pseudo"]."'");
						$_SESSION["id"] = $res_id["id"];
						$_SESSION["level"] = 0;
						$_SESSION["panel"] = 1;
						$_SESSION["canal"] = 1;
						$this->onload = "1"; $this->level = true;
					}
					else {
						$pseudo = htmlentities($_GET["i2"]);
						$res = $mysql->resultFun("count", "users", "users='".$pseudo."'");
						if ($res == 1) { $this->onload = "3"; $this->level = false; }
						else { $this->onload = "2"; $this->level = false; }
					}
					$this->address = "i";
					break;
				//validation de la suppression du compte
				case "asupv" :
					$code = htmlentities($_GET["i1"]);
					$mysqlsupv = new MysqlClass(0);
					$res = $mysqlsupv->resultFun("count", "unvalid_users", "code='".$code."'");
					if ($res == 1) {
						//suppression des données
						$mysqlsupv2 = new MysqlClass(2);
						$mysqlsupv2->resultFun("delete", "unvalid_users", "code='".$code."'");
						//finalisation
						$this->onload = "6"; $this->level = false;
					}
					else {
						$pseudo = htmlentities($_GET["i2"]);
						$res = $mysqlsupv->resultFun("count", "users", "users='".$pseudo."'");
						if ($res == 1) { $this->onload = "5"; $this->level = false; }
						else { $this->onload = "4"; $this->level = false; }
					}
					$this->address = "i";
					break;
				//récupération du compte
				case "asupn" :
					$code = htmlentities($_GET["i1"]);
					$pseudo = htmlentities($_GET["i2"]);
					$mysqlsupn = new MysqlClass(0);
					$res = $mysqlsupn->resultFun("count", "unvalid_users", "code='".$code."'");
					if ($res == 1) {
						//récupération des données
						$res = $mysqlsupn->resultFun("select", "unvalid_users", "code='".$code."'");
						$pwd_tab_alp = array ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o", "p","q", "r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
						$pwd_tab = array_rand($pwd_tab_alp, 8);
						$pwd = "";
						foreach ($pwd_tab as $elt) $pwd .= $pwd_tab_alp[$elt];
						$mysqlsupn2 = new MysqlClass(2);
						$mysqlsupn2->resultFun("insert", "users", "(users,password,email,level) values('".$pseudo."','".md5(hash("sha256", $pseudo) . hash("sha256", $pwd))."','".$res["email"]."',0)");
						$mysqlsupn2->resultFun("delete", "unvalid_users", "code='".$code."'");
						//envoi du mail
						require_once 'php/mail.php';
						mailSend($res["email"],"Arcans Project : Récupération de compte",array("validsupp",$pseudo,$pwd),$this->infos["seek"]);
						//connexion
						$_SESSION["pseudo"] = $pseudo;
						$res_id = $mysqlsupn->resultFun("select","users","users='".$_SESSION["pseudo"]."'");
						$_SESSION["id"] = $res_id["id"];
						$_SESSION["level"] = 0;
						$_SESSION["panel"] = 1;
						$_SESSION["canal"] = 1;
						//finalisation
						$this->onload = "9"; $this->level = true;
					}
					else {
						$res = $mysqlsupn->resultFun("count", "users", "users='".$pseudo."'");
						if ($res == 1) { $this->onload = "8"; $this->level = false; }
						else { $this->onload = "7"; $this->level = false; }
					}
					$this->address = "i";
					break;
				//cas de la page à adresse vierge *****************************************************
				case "" :
					$this->address = "i"; $this->onload = "0";
					if (isset($_SESSION["pseudo"])) $this->level = true;
					else $this->level = false;
					break;
				//cas par défaut **********************************************************************
				default :
					$this->address = $page; $this->onload = "0";
					if (isset($_SESSION["pseudo"])) $this->level = true;
					else $this->level = false;
					break;
			}
			if (isset($_SESSION["panel"])) {
				$this->onload = $this->onload . "," . $_SESSION["panel"] . "," . $_SESSION["canal"];
				$mysql_mail = new MysqlClass(0);
				$res_mysql = $mysql_mail->resultFun("count","dest_table","id_dest=".$_SESSION["id"]." and state=0");
				if ($res_mysql != 0) {
					$this->messages = $res_mysql;
					$this->onload = $this->onload . "," . $this->messages;
				}
				else $this->messages = "";
			}
			else $this->onload = "";
			//récupération des informations xml
			if (preg_match("/^[a]+/",$this->address)) $this->page_opt = 1;
			else {
				if ($this->address == "sep_mess") {
					$this->page_opt = 2;
					$style_xml = $infos_xml->getElementsByTagName("style_panel")->item(0);
					$this->infos["style"] = $style_xml->getAttribute("value");
				}
				else {
					$this->page_opt = 0;
					$style_xml = $infos_xml->getElementsByTagName("style_classic")->item(0);
					$this->infos["style"] = $style_xml->getAttribute("value");
				}
				$title_xml = $infos_xml->getElementsByTagName("title")->item(0);
				$this->infos["title"] = $title_xml->getAttribute("value");
				$charset_xml = $infos_xml->getElementsByTagName("charset")->item(0);
				$this->infos["charset"] = $charset_xml->getAttribute("value");
			}
			$this->initFun();
		}
		public function affichage() {
			if ($this->page_opt == 1) echo $this->page;
			elseif ($this->page_opt == 2) {
				echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
				echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>';
				$this->header->affichage();
				echo '</head><body ';
				if ($_SESSION["panel"] == 4) echo 'onload="javascript:onloadFun(\'messages\');"';
				if ($_SESSION["panel"] == 5) echo 'onload="javascript:onloadFun(\'chat\');"';
				echo '><div>';
				$mysql_mail = new MysqlClass(0);
				$res_mysql = $mysql_mail->resultFun("count","dest_table","id_dest=".$_SESSION["id"]." and state=0");
				if ($res_mysql != 0) $message = "(".$res_mysql.")";
				else $message = "";
				if ($_SESSION["panel"] == 4) {
					echo '<span class="underline" id="bt_chat">';
					echo '<button onclick="javascript:bl_panel.transition(\'chat\');">Chat</button></span>';
					if ($message != "") echo '<span class="active" id="bt_message"><b>Messagerie '.$message.'</b></span>';
					else '<span class="active" id="bt_message">Messagerie</span>';
				}
				else {
					echo '<span class="active" id="bt_chat">Chat</span>';
					echo '<span class="underline" id="bt_message">';
					echo '<button onclick="javascript:bl_panel.transition(\'messages\');">Messagerie'.$message.'</button></span>';
				}
				echo '<span class="right"><button onclick="javascript:window.close();">Réintégrer</button></span></div>';
				echo '<div style="clear:both;"></div></body></html>';
				echo '<div id="panel"><span></span></div>';
			}
			else {
				echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
				echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>';
				$this->header->affichage();
				echo '</head><body onload="javascript:onload_script('.$this->onload.');">';
				echo '<table id="entete"><tr>';
				$this->entete->affichage();
				$this->menu->affichage();
				echo '</tr></table>';
				$this->page->affichage();
				echo '<div class="foot"><table><tr><td><a href="?p=vrs">version 0.3</a></td>';
				echo '<td><a href="http://arcans-project.blogspot.com/" target="blank">Le blog du développeur</a></td>';
				echo '<td><a href="?p=faq">FAQ</a></td>';
				echo '<td><a href="?p=ctt">Nous contacter</a></td></tr></table></div>';
				echo '<div class="foot"><table><tr>';
				echo '<td><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FArcans-Project%2F161520330551295&amp;layout=button_count&amp;show_faces=true&amp;width=300&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px;" allowTransparency="true"></iframe></td>';
				echo '<td><object data="svg/gpl-v3-logo.svg" width="90" height="50" type="image/svg+xml" /></td>';
				echo '<td><a href="http://arcans-project.blogspot.com" target="_blank"><img src="http://buttons.blogger.com/blogger-simple-kahki.gif" /></a></td>';
				echo '<td><a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-via="ArcansProject" data-lang="fr">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></td>';
				echo '</tr></table></div>';
				if (isset($_SESSION["panel"])) {
					$mysql_mail = new MysqlClass(0);
					$res_mysql = $mysql_mail->resultFun("count","dest_table","id_dest=".$_SESSION["id"]." and state=0");
					if ($res_mysql != 0) $message = " (".$res_mysql.")";
					else $message = "";
					echo '<div id="bt_panel"'; if ($_SESSION["panel"] >= 4) echo ' style="display:none;"';
					echo '><button id="bt_messages" onclick="javascript:alert(\'élément non implémenté\');">Messagerie'.$message.'</button>';
					echo '<button id="bt_chat" onclick="javascript:panel.ouverture(\'chat\');">Chat</button></div>';
					echo '<div id="panel" style="display:none;"></div>';
				}
				echo '<div id="voile" style="display:none;"></div><div id="encart" style="display:none;"></div>';
				echo '</body></html>';
			}
		}
			//méthodes privées :
		private function initFun() {
			if ($this->page_opt == 1) {
				require_once 'php/ajax.php';
				require_once 'php/mail.php';
				$this->page = AjaxReponseClass::initAjaxFun($this->address, $this->infos["seek"]);
			}
			elseif ($this->page_opt == 2) {
				require_once 'php/entete.php';
				if ($_SESSION["panel"] == 4) $titre = "Messagerie";
				elseif ($_SESSION["panel"] == 5) $titre = "Chat";
				$this->header = new HeaderClass('panel','', $titre, $this->infos);
			}
			else {
				require_once 'php/entete.php';
				require_once 'php/menu.php';
				require_once 'php/page.php';
				require_once 'php/xml.php';
				$this->xmlinfo = new ObjXmlClass($this->level, $this->address);
				$this->header = new HeaderClass('classic',$this->xmlinfo->infoHead(), $this->xmlinfo->pageTitle(), $this->infos);
				$this->entete = new EnteteClass();
				$this->menu = new MenuClass($this->xmlinfo->infoMenu());
				$this->page = new PageClass($this->xmlinfo->structText());
			}
		}
	}
	//******************************
	//script de gestion des requêtes
	//******************************
	session_start();
	require_once 'php/mysql.php';
	$rootObj = new MainClass();
	$rootObj->affichage();
?>